﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using BYFW.DAL;
using BYFW.BOL;
using BYFW.SFL;

namespace BYFW.WebBase
{
    /// <summary>
    /// 用于查询页面的页面信息类，缓存页面信息，使之可跳转且保持页面信息
    /// </summary>
    public class PageInfo
    {
        /// <summary>
        /// 保护的构造函数
        /// </summary>
        /// <param name="page">指定的页面</param>
        /// <param name="specialCode">页面的特征码，用于一个页面需要多个信息类时区分</param>
        /// <param name="dbop">数据库操作类</param>
        protected PageInfo(Page page, string specialCode, IDBOP dbop)
        {
            m_page = page;
            SpecialCode = specialCode;
            m_dbop = dbop;
            this.InitInstance();
        }
        /// <summary>
        /// 获取指定页面的页面信息类
        /// </summary>
        /// <param name="page">指定的页面</param>
        /// <returns></returns>
        public static PageInfo GetPageInfo(Page page)
        {
            return GetPageInfo(page, "", new BaseEntity().DB);
        }
        /// <summary>
        /// 获取指定页面的页面信息类
        /// </summary>
        /// <param name="page">指定的页面</param>
        /// <param name="specialCode">页面的特征码，用于一个页面需要多个信息类时区分</param>
        /// <param name="dbop">数据库操作类</param>
        /// <returns></returns>
        public static PageInfo GetPageInfo(Page page, string specialCode, IDBOP dbop)
        {
            string key = page.GetType().Name + specialCode;
            if (page.Session[key] == null)
            {
                page.Session[key] = new PageInfo(page, specialCode, dbop);

            }
            return (PageInfo)page.Session[key];
        }

        /// <summary>
        /// 过滤信息数组
        /// </summary>
        protected List<FilterInfo> m_filterInfo = new List<FilterInfo>();
        /// <summary>
        /// 额外信息字典
        /// </summary>
        protected Dictionary<string, object> m_info = new Dictionary<string, object>();
        protected List<DropDownList> m_opList = new List<DropDownList>();
        protected List<DropDownList> m_fldList = new List<DropDownList>();
        protected List<DropDownList> m_calList = new List<DropDownList>();
        protected List<TextBox> m_txtList = new List<TextBox>();
        protected Page m_page = null;
        protected IDBOP m_dbop = null;
        protected bool m_isFirst = true;

        /// <summary>
        /// 是否是初始化页面数据，只有在本页面信息类被Cache后才变成non-first
        /// </summary>
        public bool IsFirst
        {
            get { return m_isFirst; }
        }
        /// <summary>
        /// 排序字符串
        /// </summary>
        public string Sort
        {
            get;
            set;
        }
        /// <summary>
        /// 排序方向
        /// </summary>
        public bool Direction
        {
            get;
            set;
        }
        /// <summary>
        /// 本实例的特征信息码，用于一个页面使用多个实例时
        /// </summary>
        public string SpecialCode
        {
            get;
            set;
        }
        /// <summary>
        /// 页面视图保存的过滤条件
        /// </summary>
        public string Condition
        {
            get;
            set;
        }
        /// <summary>
        /// 页面视图保存的过滤条件，包含Where关键字
        /// </summary>
        public string ConditionWhere
        {
            get { return Condition.Trim() == "" ? "" : " Where " + Condition; }
        }
        /// <summary>
        /// 页面显示记录的大小
        /// </summary>
        public int PageSize
        {
            get;
            set;
        }
        /// <summary>
        /// 页面显示记录的页数
        /// </summary>
        public int PageIndex
        {
            get;
            set;
        }
        /// <summary>
        /// 过滤信息数组
        /// </summary>
        public List<FilterInfo> FilterInfo
        {
            get { return m_filterInfo; }
        }
        /// <summary>
        /// 额外信息字典
        /// </summary>
        public Dictionary<string, object> Info
        {
            get { return m_info; }
        }
        /// <summary>
        /// 页面与或操作下拉框控件列表
        /// </summary>
        public List<DropDownList> OPList
        {
            get { return m_opList; }
        }
        /// <summary>
        /// 页面字段操作下拉框控件列表
        /// </summary>
        public List<DropDownList> FldList
        {
            get { return m_fldList; }
        }
        /// <summary>
        /// 页面条件操作下拉框控件列表
        /// </summary>
        public List<DropDownList> CalList
        {
            get { return m_calList; }
        }
        /// <summary>
        /// 页面值文本框控件列表
        /// </summary>
        public List<TextBox> TxtList
        {
            get { return m_txtList; }
        }
        /// <summary>
        /// 当前实例所属的页面
        /// </summary>
        public Page Page
        {
            get { return m_page; }
        }
        /// <summary>
        /// 使用的数据库操作类
        /// </summary>
        public IDBOP DBOP
        {
            get { return m_dbop; }
        }

        protected void InitDDLOP()
        {
            for (int i = 0; i < m_opList.Count; i++)
            {
                m_opList[i].Items.Clear();
                m_opList[i].Items.Add(new ListItem("且", " and "));
                m_opList[i].Items.Add(new ListItem("或", " or "));
                m_opList[i].SelectedIndex = 0;
            }
        }
        protected void InitDDLCal()
        {
            for (int i = 0; i < m_calList.Count; i++)
            {
                m_calList[i].Items.Clear();
                m_calList[i].Items.Add(" ");
                m_calList[i].Items.Add(" in ");
                m_calList[i].Items.Add(" not in ");
                m_calList[i].Items.Add(" between ");
                m_calList[i].Items.Add(" multi and");
                m_calList[i].Items.Add(" multi or");
                m_calList[i].Items.Add(" = ");
                m_calList[i].Items.Add(" <> ");
                m_calList[i].Items.Add(" like ");
                m_calList[i].Items.Add(" not like ");
                m_calList[i].Items.Add(" <= ");
                m_calList[i].Items.Add(" < ");
                m_calList[i].Items.Add(" >= ");
                m_calList[i].Items.Add(" > ");
                m_calList[i].SelectedIndex = 0;
            }
        }
        protected void InitDDLFld()
        {
            for (int i = 0; i < m_fldList.Count; i++)
            {
                m_fldList[i].Items.Clear();
            }
        }
        protected void InitTxt()
        {
            for (int i = 0; i < m_txtList.Count; i++)
            {
                m_txtList[i].Text = "";
            }
        }
        /// <summary>
        /// 初始化页面信息类中使用的过滤信息列表
        /// </summary>
        protected void InitFilterInfo()
        {
            //按照页面实际过滤条件的长度补足过滤信息类的长度
            int delta = m_fldList.Count - m_filterInfo.Count;
            for (int i = 0; i < delta; i++)
            {
                FilterInfo fi = new FilterInfo();
                fi.NameIndex = i;
                fi.CalIndex = 0;
                fi.Value = "";
                fi.OpIndex = 0;
                m_filterInfo.Add(fi);
            }
        }
        /// <summary>
        /// 初始化本类的实例中的信息
        /// </summary>
        /// <returns></returns>
        protected void InitInstance()
        {
            m_isFirst = true;
            Sort = "";
            Direction = false;
            Condition = "";
            PageIndex = 0;
            PageSize = 1;
            m_info.Clear();
            m_filterInfo.Clear();
        }

        /// <summary>
        /// 清空页面控件
        /// </summary>
        public void ClearFilterControls()
        {
            m_opList.Clear();
            m_fldList.Clear();
            m_calList.Clear();
            m_txtList.Clear();
        }
        /// <summary>
        /// 初始化页面控件显示的内容
        /// </summary>
        public void InitFilterControls()
        {
            this.InitDDLOP();
            this.InitDDLCal();
            this.InitDDLFld();
            this.InitTxt();
            this.InitFilterInfo();
        }
        /// <summary>
        /// 添加过滤字段，用于页面生成过滤条件
        /// </summary>
        /// <param name="name">字段在界面的显示名</param>
        /// <param name="field">字段名，可以为表达式</param>
        /// <param name="type">字段的数据类型</param>
        /// <param name="needQuote">字段是否需要引号括住</param>
        /// <param name="specialInfo">添加的额外信息</param>
        public void AddFilterField(string name, string field, EDataType type, bool needQuote,string specialInfo)
        {
            field = needQuote ? m_dbop.GetCol(field) : field;
            field += "|" + ((int)type).ToString() + (string.IsNullOrEmpty(specialInfo) ? "" : ("|" + specialInfo));
            for (int i = 0; i < m_fldList.Count; i++)
            {
                m_fldList[i].Items.Add(new ListItem(name, field));
            }
        }
        /// <summary>
        /// 为页面使用的条件，解析出字段以及数据类型
        /// </summary>
        /// <param name="field"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string ParseFiterFieldForPageConditon(string field, out EDataType type)
        {
            type = EDataType.String;
            string res = "";
            string[] tmp = field.Split('|');
            if (tmp.Length > 0)
            {
                res = tmp[0];
            }
            if (tmp.Length > 1)
            {
                try
                {
                    type = (EDataType)CommonOP.GetInt(tmp[1], true);
                }
                catch { }
            }
            return res;
        }
        /// <summary>
        /// 获取当前的过滤条件，不具有Where等关键字
        /// </summary>
        /// <returns></returns>
        public string GetCondition()
        {
            string cond = "";
            for (int i = 0; i < m_calList.Count; i++)
            {
                if (m_calList[i].SelectedIndex > 0)
                {
                    if (i > 0 && cond.Trim() != "")
                    {
                        cond += m_opList[i - 1].SelectedValue;
                    }
                    string txt = m_txtList[i].Text;
                    string field = m_fldList[i].SelectedValue;
                    EDataType type = EDataType.String;
                    field = ParseFiterFieldForPageConditon(field, out type);

                    //对输入的数据进行初步验证
                    switch (type)
                    {
                        case EDataType.Enum:
                        case EDataType.Float:
                        case EDataType.Int:
                        case EDataType.Number:
                            txt = CommonOP.GetDouble(txt, false).ToString();
                            break;
                    }

                    //IN和NOT IN
                    if (m_calList[i].SelectedIndex == 1 || m_calList[i].SelectedIndex == 2)
                    {
                        cond += m_dbop.GetSQLInAndNotInCondition(field, type, txt, m_calList[i].SelectedIndex == 1, false);
                    }
                    //BETWEEN
                    else if (m_calList[i].SelectedIndex == 3)
                    {
                        string[] txts = txt.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                        if (txts.Length > 1)
                        {
                            cond += m_dbop.GetSQLBetweenCondition(field, type, txts[0], txts[1], false);
                        }
                    }
                    //MULTI AND/OR
                    else if (m_calList[i].SelectedIndex == 4 || m_calList[i].SelectedIndex == 5)
                    {
                        cond += m_dbop.GetSQLMultiCondition(field, type, txt, m_calList[i].SelectedIndex == 4, false);
                    }
                    //NORMAL
                    else
                    {
                        cond += m_dbop.GetSQLNormalCondition(field, type, txt, m_calList[i].Text, false);
                    }
                }
            }
            Condition = cond;
            return cond;
        }
        /// <summary>
        /// 获取具有方向的排序表达式
        /// </summary>
        /// <returns></returns>
        public string GetSortExpression()
        {
            return Sort.Trim() == "" ? "" : Direction ? Sort + " asc" : Sort + " desc";
        }
        /// <summary>
        /// 清除设置的过滤条件
        /// </summary>
        public void ClearPageInfo()
        {
            this.InitInstance();
            this.InitFilterInfo();
        }
        /// <summary>
        /// 缓存页面操作时设置的过滤条件
        /// </summary>
        public void CachePageFilterControlInfo()
        {
            m_isFirst = false;
            for (int i = 0; i < FilterInfo.Count; i++)
            {
                FilterInfo[i].NameIndex = m_fldList[i].SelectedIndex;
                FilterInfo[i].CalIndex = m_calList[i].SelectedIndex;
                if (i > 0)
                {
                    FilterInfo[i].OpIndex = m_opList[i - 1].SelectedIndex;
                }
                FilterInfo[i].Value = m_txtList[i].Text;
            }
        }
        /// <summary>
        /// 根据页面信息类设置页面
        /// </summary>
        public void InitPageFilterControlByPageInfo()
        {
            for (int i = 0; i < FilterInfo.Count; i++)
            {
                m_fldList[i].SelectedIndex = (FilterInfo[i].NameIndex >= m_fldList[i].Items.Count ? m_fldList[i].Items.Count - 1 : FilterInfo[i].NameIndex);
                m_calList[i].SelectedIndex = FilterInfo[i].CalIndex;
                if (i > 0)
                {
                    m_opList[i - 1].SelectedIndex = FilterInfo[i].OpIndex;
                }
                m_txtList[i].Text = FilterInfo[i].Value;
            }
        }
        /// <summary>
        /// 设置页面额外需要缓存的信息
        /// </summary>
        /// <param name="key"></param>
        /// <param name="val"></param>
        public void SetInfo(string key, object val)
        {
            if (m_info.ContainsKey(key))
            {
                m_info[key] = val;
            }
            else
            {
                m_info.Add(key, val);
            }
        }
        /// <summary>
        /// 获取页面额外的缓存信息
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public object GetInfo(string key)
        {
            if (m_info.ContainsKey(key))
            {
                return m_info[key];
            }
            else
            {
                return null;
            }
        }
    }

    /// <summary>
    /// 页面过滤信息
    /// </summary>
    public class FilterInfo
    {
        /// <summary>
        /// 页面过滤信息类
        /// </summary>
        public FilterInfo()
        {
        }

        /// <summary>
        /// 名称索引
        /// </summary>
        public int NameIndex
        {
            get;
            set;
        }
        /// <summary>
        /// 操作符索引
        /// </summary>
        public int OpIndex
        {
            get;
            set;
        }
        /// <summary>
        /// 计算符索引
        /// </summary>
        public int CalIndex
        {
            get;
            set;
        }
        /// <summary>
        /// 值
        /// </summary>
        public string Value
        {
            get;
            set;
        }
    }
}
